package com.briup.web.servlet;

import com.briup.bean.Customer;
import com.briup.bean.ShopCar;
import com.briup.service.IShopCarService;
import com.briup.service.impl.ShopCarServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ShopCarAddServlet", value = "/shopCarAdd")
public class ShopCarAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        IShopCarService shopCarService = new ShopCarServiceImpl();
        Integer num = Integer.valueOf(req.getParameter("num"));
        Integer bookId = Integer.valueOf(req.getParameter("bookId"));
        HttpSession session = req.getSession();
        Customer customer = (Customer) session.getAttribute("customer");
//        customer.getId()通过这个方法获取到用户的id
        Integer customerId = customer.getId();
//        把num、bookId、customerId封装成一个ShopCar类型
        ShopCar shopCar = new ShopCar(num,bookId,customerId);
//        调用service层的saveShopCar方法
        shopCarService.saveShopCar(shopCar);

//        查询出当前用户的所有购物车记录
        List<ShopCar> shopCarList = shopCarService.findShopCarsByCustomer(customerId);
//          把结果集放进session里
        session.setAttribute("shopCarList",shopCarList);
//        跳转到shopCar.jsp
        resp.sendRedirect("/shopCar.jsp");
    }
}
